package com.boardgame.gwt.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.boardgame.gwt.data.GwtGameBoard;
import com.boardgame.gwt.data.GwtPlayerInfo;
import com.boardgame.gwt.data.card.*;
import com.boardgame.gwt.data.card.cattle.*;
import com.boardgame.gwt.data.card.objective.BaseObjectiveCard;
import com.boardgame.gwt.data.card.objective.ObjectiveCard;
import com.boardgame.gwt.data.card.objective.ObjectiveCardInitial;
import com.boardgame.gwt.data.space.*;
import com.boardgame.gwt.data.tile.GwtSpaceTile;
import com.boardgame.gwt.data.tile.building.GwtBuildingTile;
import com.boardgame.gwt.data.tile.building.Initial.*;
import com.boardgame.gwt.data.tile.hazard.GwtSpaceTileHazardDrought;
import com.boardgame.gwt.data.tile.hazard.GwtSpaceTileHazardFlood;
import com.boardgame.gwt.data.tile.hazard.GwtSpaceTileHazardRockfall;
import com.boardgame.gwt.data.tile.teepee.GwtSpaceTileTeepeeBlue;
import com.boardgame.gwt.data.tile.teepee.GwtSpaceTileTeepeeGreen;
import com.boardgame.gwt.data.tile.worker.*;
import com.boardgame.gwt.entity.GwtGameDbInfo;
import com.boardgame.gwt.entity.GwtPlayerDbInfo;
import com.boardgame.gwt.finalinfo.TileNumberFinal;
import com.boardgame.gwt.repository.GwtPlayerRepository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 游戏初始化
 */
public class GameInitial {
    private GwtGameBoard gwtGameBoard;
    public GameInitial(GwtGameBoard gwtGameBoard){
        this.gwtGameBoard = gwtGameBoard;
    }

    /**
     * 创建游戏
     * @return
     */
    public static GwtGameBoard create(GwtGameDbInfo gwtGameInfo, GwtPlayerRepository gwtPlayerRepository){

        GwtGameBoard gwtGameBoard = JSON.parseObject(JSON.toJSONString(gwtGameInfo), new TypeReference<GwtGameBoard>() {});

        //地图空格
        List<GwtSpace> listSpace = new ArrayList<>();
        //灾难
        listSpace.addAll(GwtSpaceHazard.createHazardList());
        //建筑
        List<GwtSpace> listBuildings= GwtSpaceBuilding.createSpaceBuilding();
        listSpace.addAll(listBuildings);

        //帐篷
        listSpace.addAll(GwtSpaceTeepee.createSpaceTent());
        gwtGameBoard.setListSpace(listSpace);
        //铁路
        gwtGameBoard.setListRailroadSpace(GwtSpaceRailroad.createSpaceRailroad());

        //初始化
        GameInitial gameInitial = new GameInitial(gwtGameBoard);
        gameInitial.initData(gwtPlayerRepository);

        //返回
        return gwtGameBoard;
    }

    /**
     * 初始化
     * @param gwtPlayerRepository
     */
    public void initData(GwtPlayerRepository gwtPlayerRepository){
        this.createPlayers(gwtPlayerRepository);
        //初始化建筑
        this.createInitialBuildings();
        //初始化tile
        this.createInitialStartTiles();
        //初始化牛市卡
        this.createCattleCard();
        //初始化进站信息
        this.craeteForesightSpace();
        //创建任务
        this.createObjectiveCard();

    }

    /**
     * 虚构4名玩家
     */
    private void createPlayers(GwtPlayerRepository gwtPlayerRepository){
        this.gwtGameBoard.setListPlayer(new ArrayList<>());
        //GwtPlayer player = new GwtPlayer();
        GwtPlayerDbInfo gwtPlayerDbInfo1=this.createGwtPlayer(gwtPlayerRepository,"ala","yellow");
        this.gwtGameBoard.getListPlayer().add(JSON.parseObject(JSON.toJSONString(gwtPlayerDbInfo1), new TypeReference<GwtPlayerInfo>() {}));
        GwtPlayerDbInfo gwtPlayerDbInfo2=this.createGwtPlayer(gwtPlayerRepository,"second","red");
        GwtPlayerDbInfo gwtPlayerDbInfo3=this.createGwtPlayer(gwtPlayerRepository,"third","blue");
        GwtPlayerDbInfo gwtPlayerDbInfo4=this.createGwtPlayer(gwtPlayerRepository,"forth","green");
        this.gwtGameBoard.getListPlayer().add(JSON.parseObject(JSON.toJSONString(gwtPlayerDbInfo2), new TypeReference<GwtPlayerInfo>() {}));
        this.gwtGameBoard.getListPlayer().add(JSON.parseObject(JSON.toJSONString(gwtPlayerDbInfo3), new TypeReference<GwtPlayerInfo>() {}));
        this.gwtGameBoard.getListPlayer().add(JSON.parseObject(JSON.toJSONString(gwtPlayerDbInfo4), new TypeReference<GwtPlayerInfo>() {}));

        //任务卡
        List<ObjectiveCardInitial> listObjective = ObjectiveCardInitial.create();
        this.setShuffleList(listObjective);

        //起始金钱
        int startCoin=6;
        //初始化玩家手牌
        for(GwtPlayerInfo playerInfo:this.gwtGameBoard.getListPlayer()){
            playerInfo.setCoins(startCoin);
            //发一张任务卡
            playerInfo.getListCardObjective().add(listObjective.get(startCoin - 6));
            for(int i=0;i<5;i++){
                playerInfo.getListCardPile().add(new Card1Gray(i));
            }
            for(int i=0;i<3;i++){
                playerInfo.getListCardPile().add(new Card2Black(5+i));
                playerInfo.getListCardPile().add(new Card2Green(8+i));
                playerInfo.getListCardPile().add(new Card2White(11+i));
            }
            //随机洗混
            Collections.shuffle(playerInfo.getListCardPile());
            System.out.println("手牌数量:"+playerInfo.getListCardPile().size());
            //摸4张到手上
            playerInfo.getListCardHand().addAll(playerInfo.getListCardPile().subList(0,4));
            //移除摸到的手牌
            for(int i=0;i<4;i++){
                playerInfo.getListCardPile().remove(0);
            }
            startCoin++;
        }



    }

    /**
     * 设置初始的建筑板块
     */
    private void createInitialBuildings(){
        List<GwtBuildingTile> list = new ArrayList<>();
        list.add(new GWTBuildingA());
        list.add(new GWTBuildingB());
        list.add(new GWTBuildingC());
        list.add(new GWTBuildingD());
        list.add(new GWTBuildingE());
        list.add(new GWTBuildingF());
        list.add(new GWTBuildingG());

        //随机洗混
        Collections.shuffle(list);
        //分配到相应位置
        int[][] pos= {
                {1,0},{2,0},{5,0},{7,1},{9,0},{12,0},{14,1}
        };
        for (int i=0;i<7;i++){
            int[] item = pos[i];
            GwtSpace gwtSplace = this.gwtGameBoard.getGwtSplace(item[0], item[1]);
            gwtSplace.setGwtSpaceTile(list.get(i));
        }

    }


    /**
     * 创建玩家
     */
    private GwtPlayerDbInfo createGwtPlayer(GwtPlayerRepository gwtPlayerRepository, String username, String color){
        GwtPlayerDbInfo gwtPlayer = new GwtPlayerDbInfo();
        gwtPlayer.setUserName(username);
        gwtPlayer.setColor(color);
        gwtPlayer.setPaygrade(0);
        gwtPlayer.setCreateUser(21);
        //移动步数
        if (this.gwtGameBoard.getPlayerNumber()==3){
            gwtPlayer.setMoveSpaces(4);
        }
        else if (this.gwtGameBoard.getPlayerNumber()==4){
            gwtPlayer.setMoveSpaces(3);
        }
        else if (this.gwtGameBoard.getPlayerNumber()==2){
            gwtPlayer.setMoveSpaces(4);
        }
        //gwtPlayer.set
        GwtPlayerDbInfo dbInfo = (GwtPlayerDbInfo)gwtPlayer;
        GwtPlayerDbInfo save = gwtPlayerRepository.save(dbInfo);
        return gwtPlayer;
    }

    /**
     * 设置游戏开始的板块
     */
    public void createInitialStartTiles(){
        this.gwtGameBoard.setListTileFirst(new ArrayList<>());
        this.gwtGameBoard.setListTileSecond(new ArrayList<>());
        this.gwtGameBoard.setListTileThird(new ArrayList<>());
        //第一级别
        //绿色和蓝色帐篷
        for (int i = 0; i< TileNumberFinal.NUMBER_FIRST_BLUE_TEEPE; i++){
            this.gwtGameBoard.getListTileFirst().add(new GwtSpaceTileTeepeeBlue());
            this.gwtGameBoard.getListTileFirst().add(new GwtSpaceTileTeepeeGreen());
        }
        //灾难
        for (int i=0;i< TileNumberFinal.NUMBER_EACH_HAZARD;i++){
            this.gwtGameBoard.getListTileFirst().add(new GwtSpaceTileHazardFlood());
            this.gwtGameBoard.getListTileFirst().add(new GwtSpaceTileHazardDrought());
            this.gwtGameBoard.getListTileFirst().add(new GwtSpaceTileHazardRockfall());
        }
        this.setShuffleList(this.gwtGameBoard.getListTileFirst());
        //第二级工人
        for (int i=0;i< TileNumberFinal.NUMBER_SECOND_GREEN_COWBOY;i++){
            this.gwtGameBoard.getListTileSecond().add(new GwtSpaceTileWorkerCowboy());
            this.gwtGameBoard.getListTileSecond().add(new GwtSpaceTileWorkerCraftsman());
            this.gwtGameBoard.getListTileSecond().add(new GwtSpaceTileWorkerEngineer());
        }
        this.setShuffleList(this.gwtGameBoard.getListTileSecond());
        //第三极别
        for (int i=0;i< TileNumberFinal.NUMBER_SECOND_GREEN_COWBOY;i++){
            this.gwtGameBoard.getListTileThird().add(new GwtSpaceTileWorkerCowboy());
            this.gwtGameBoard.getListTileThird().add(new GwtSpaceTileWorkerCraftsman());
            this.gwtGameBoard.getListTileThird().add(new GwtSpaceTileWorkerEngineer());
        }
        this.setShuffleList(this.gwtGameBoard.getListTileThird());

        //从第一级别抽出7个
        for (int i=0;i<7;i++){
            GwtSpaceTile gwtSpaceTile = this.gwtGameBoard.getListTileFirst().remove(0);
            //添加到版图
            gwtSpaceTile.addTileToMapSpace(this.gwtGameBoard);
        }
        //创建工人格子
        this.gwtGameBoard.setListSpaceWorker(new ArrayList<>());
        List<GwtSpace> listWorker = this.gwtGameBoard.getListSpaceWorker();
        for (int i=0;i<12;i++){
            for (int number = 0 ;number<this.gwtGameBoard.getPlayerNumber();number++){
                GwtSpaceWorker gwtSpaceWorker = new GwtSpaceWorker();
                gwtSpaceWorker.setRow(i);
                gwtSpaceWorker.setColumn(number);
                listWorker.add(gwtSpaceWorker);
            }
        }
        //绑定空格和工人
        int workers=this.gwtGameBoard.getPlayerNumber()*2-1;
        for (int i=0;i<workers;i++){
            GwtSpaceTileWorker gwtSpaceTile = this.gwtGameBoard.getListTileSecond().remove(0);
            listWorker.get(i).setGwtSpaceTile(gwtSpaceTile);
        }
        listWorker.get(workers).setOffset(1);

        //从第二级别抽出玩家人数-1个工人
//        this.gwtGameBoard.setListTileWorker( new ArrayList<>());
//        List<GwtBoardTileWorker> listWorker = this.gwtGameBoard.getListTileWorker();
//
//


    }


    /**
     * 创建卡牌
     */
    private void createCattleCard(){
        this.gwtGameBoard.setListCattleCardPile(new ArrayList<>());
        //3
        for (int i=0;i<7;i++){
            this.gwtGameBoard.getListCattleCardPile().add(new Card3Yellow());
            this.gwtGameBoard.getListCattleCardPile().add(new Card3Red());
            this.gwtGameBoard.getListCattleCardPile().add(new Card3Blue());
        }
        //4
        for (int i=0;i<9;i++){
            this.gwtGameBoard.getListCattleCardPile().add(new Card4Brown());
        }
        for (int i=0;i<6;i++){
            this.gwtGameBoard.getListCattleCardPile().add(new Card5Purple());
        }
        this.setShuffleList(this.gwtGameBoard.getListCattleCardPile());
        //抽出卡帕
        int cardNumber = 13;
        List<BaseCard> list = new ArrayList<>();
        list.addAll(this.gwtGameBoard.getListCattleCardPile().subList(0,cardNumber));
        for (int i=0;i<cardNumber;i++){
            this.gwtGameBoard.getListCattleCardPile().remove(i);
        }
        this.gwtGameBoard.setListCattleCardMarket(list);
    }


    /**
     * 先见空格
     */
    private void craeteForesightSpace(){
        this.gwtGameBoard.setListSpaceForesight(new ArrayList<>());
        for (int row= 0 ;row<3;row++){
            for (int column = 0;column<2;column++){
                GwtSpaceForesight gwtSpaceForesight = new GwtSpaceForesight();
                gwtSpaceForesight.setRow(row);
                gwtSpaceForesight.setColumn(column);
                //分别填满6个空格
                GwtSpaceTile gwtSpaceTile = null;
                switch (row){
                    case 0:
                        gwtSpaceTile = this.gwtGameBoard.getListTileFirst().remove(0);
                        break;
                    case 1:
                        gwtSpaceTile = this.gwtGameBoard.getListTileSecond().remove(0);
                        break;
                    case 2:
                        gwtSpaceTile = this.gwtGameBoard.getListTileThird().remove(0);
                        break;
                    default:
                        gwtSpaceForesight=null;
                }
                gwtSpaceForesight.setGwtSpaceTile(gwtSpaceTile);
                this.gwtGameBoard.getListSpaceForesight().add(gwtSpaceForesight);
            }
        }


    }

    /**
     * 初始化任务市场
     */
    private void createObjectiveCard(){
        //洗混任务
        for (int i=0;i<24;i++){
            ObjectiveCard objectiveCard = new ObjectiveCard(i+1);
            this.gwtGameBoard.getListCardObjectPile().add(objectiveCard);
        }
        this.setShuffleList(this.gwtGameBoard.getListCardObjectPile());
        //翻出4张任务
        for (int i=0;i<4;i++){
            BaseObjectiveCard baseObjectiveCard = this.gwtGameBoard.getListCardObjectPile().remove(0);
            this.gwtGameBoard.getListCardObjectMarket().add(baseObjectiveCard);
        }
   }

    /**
     * 洗混列表
     * @param list
     */
    private void setShuffleList(List list){
        Collections.shuffle(list);
    }

}
